﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            ShapeFactory circleFactory = new CircleFactory();
            Shape circle = circleFactory.CreateShape();
            circle.Draw();

            ShapeFactory rectangleFactory = new RectangleFactory();
            Shape rectangle = rectangleFactory.CreateShape();
            rectangle.Draw();

            ShapeFactory squareFactory = new SquareFactory();

            Shape square = squareFactory.CreateShape();
            square.Draw();

            Console.ReadKey();
        }
    }
    /// <summary>
    /// 首先，我们需要定义一个图形接口
    /// </summary>
    public interface Shape
    {
        void Draw();
    }
    //然后，我们实现两个具体的图形类，分别是 Circle（圆形）和 Rectangle（矩形）
    public class Circle : Shape
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a Circle");
        }
    }
    public class Rectangle : Shape
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a Reactangle");
        }
    }

    public class Square : Shape
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a Square");
        }
    }

    // 接下来，我们创建一个抽象工厂类 ShapeFactory
    // 它定义了一个抽象的工厂方法 createShape，子类将实现这个方法来创建具体的图形对象
    public abstract class ShapeFactory
    {
        public abstract Shape CreateShape();
    }
    // 然后，我们创建两个具体的工厂类，分别是 CircleFactory 和 RectangleFactory
    // 它们分别实现了 ShapeFactory 并重写了 createShape 方法来返回相应的图形对象
    public class CircleFactory : ShapeFactory
    {
        public override Shape CreateShape()
        {
            return new Circle();
        }
    }
    public class RectangleFactory : ShapeFactory
    {
        public override Shape CreateShape()
        {
            return new Rectangle();
        }
    }
    public class SquareFactory : ShapeFactory
    {
        public override Shape CreateShape()
        {
            return new Square();
        }
    }
}
